/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Business;

import Model.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Nguyen Hung
 */
public class LineItem {
    private Product product;
    private int quantity;
    public LineItem(){

    }

    public Product getProduct() {
        return product;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public void setQuality(int quality) {
        this.quantity = quality;
    }

    public double getTotal(){
        return quantity * product.getPrice();
    }

    public void save(String orderid) throws SQLException{
        Connection conection = DAO.getConnection();
        String sql = "insert into orderline values(?, ?, ?)";
        PreparedStatement ps = conection.prepareStatement(sql);
        ps.setString(1, orderid);
        ps.setString(2, product.getProductCode());
        ps.setInt(3, quantity);
        ps.execute();
        ps.close();
        conection.close();

    }

    public static void main(String[] a) throws SQLException{
        LineItem item = new LineItem();
        Product product = new Product();
        product.setProductCode("Q1000C");
        product.load();
        item.setProduct(product);
        item.setQuality(12);
        System.out.println(item.getTotal());
//        item.setQuality(15);
//        item.save("1");


    }

    public LineItem(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    public String toString(){
        return product + " " + quantity;
    }

    public void updateQuantity(){
        try {
            Connection connection = DAO.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("update product set quantity = quantity - ? where productCode = ?");
            preparedStatement.setInt(1, quantity);
            preparedStatement.setString(2, product.getProductCode());
            preparedStatement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(LineItem.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}
